import {getChart,downloadHistory} from '@/services/indicator';

const Indicator = {
  namespace: 'indicator',
  state: {},
  effects: {
    * chart({payload}, {call, put}) {
      // 请求mock接口
      const response = yield call(getChart, payload);
      // dispatch转发action
      yield put({
        type: 'saveValues',
        payload: response.detail,
        name: payload.name,
      });
    },
    * downloadHistory({payload, callback}, {call, put}) {
      const response = yield call(downloadHistory, payload);
      // dispatch转发action

      if (response instanceof Blob) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
      } else {
        message.warning('Some error messages...', 5);
      }
    },
  },
  reducers: {
    //同步方法
    saveValues(state, {payload, name}) {
      const v = {...state};
      v[name] = payload;
      return v;
    },
  },
  //触发器。setup表示初始化即调用。
  //正常使用的话,初始化的数据加载应该在组件挂载的时候执行
  // 其他用法见官方文档。https://github.com/sorrycc/blog/issues/62
  // subscriptions: {
  //   setup({dispatch}) {
  //     dispatch({type: 'listFetch'})
  //   }
  // }
};
export default Indicator;
